package com.hxf.service.order.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.hxf.dto.MealOrderDetailDto;
import com.hxf.dto.MealOrderDto;
import com.hxf.dto.MealPreferentialDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.service.base.impl.BaseServiceImpl;
import com.hxf.service.order.OrderService;
import com.hxf.web.vo.OrderDetailVo;

/**
 * service 餐厅订单业务实现类
 * 
 * @author tengyicheng
 * @version [1.0, 2013/11/21]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Repository("orderService")
public class OrderServiceImpl extends BaseServiceImpl implements OrderService {
	@Override
	public ReturnData<MealOrderDto> findMealOrder(MealOrderDto orderDto,int targetPage, int pageSize) throws ServiceException {
		try {
			return super.daoFacade.getOrderDao().findMealOrder(orderDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	} 
	
	public int saveOrder(MealOrderDto orderDto, List<OrderDetailVo> orderDetailVoList) throws ServiceException {
			//保存主订单
			this.save(orderDto);
			MealOrderDetailDto orderDetailDto = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//订单详细表
			for (int i = 0;i < orderDetailVoList.size(); i++) {  
				orderDetailDto = new MealOrderDetailDto();
				orderDetailDto.setOrderId(orderDto.getOrderId());
				orderDetailDto.setMealMenuId(orderDetailVoList.get(i).getId()); 
				orderDetailDto.setCreateDate(sdf.format(new Date()));
				orderDetailDto.setQuantity(orderDetailVoList.get(i).getNum());
				orderDetailDto.setDetailType(orderDetailVoList.get(i).getType());
				orderDetailDto.setSatuts(0);
				//保存订单详细表
				this.save(orderDetailDto);
			}
		return 0;
	}
	
	public int updateOrder(MealOrderDto orderDto, List<OrderDetailVo> orderDetailVoList) throws ServiceException {
		try {
			//主订单
			MealOrderDto mod = (MealOrderDto) super.get(MealOrderDto.class, orderDto.getOrderId());
			if (mod != null) { 
				mod.setName(orderDto.getName());
				mod.setContact(orderDto.getContact());
				mod.setBoxId(orderDto.getBoxId());
				mod.setPeopleNum(orderDto.getPeopleNum());
				mod.setRepastDate(orderDto.getRepastDate());
				this.daoFacade.getOrderDao().update(mod);
			} 
			
			MealOrderDetailDto orderDetailDto = null;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//订单详细表 先删除 在添加
			this.deteleMealOrderDetailByOrderId(orderDto.getOrderId());
			
			for (int i = 0;i < orderDetailVoList.size(); i++) { 
				System.out.println(orderDetailVoList.get(i).getNum() + "-------------------------");
				orderDetailDto = new MealOrderDetailDto();
				orderDetailDto.setOrderId(orderDto.getOrderId());
				orderDetailDto.setMealMenuId(orderDetailVoList.get(i).getId()); 
				orderDetailDto.setCreateDate(sdf.format(new Date()));
				orderDetailDto.setQuantity(orderDetailVoList.get(i).getNum());
				orderDetailDto.setDetailType(orderDetailVoList.get(i).getType());
				orderDetailDto.setSatuts(0);
				//保存订单详细表
				this.daoFacade.getOrderDao().create(orderDetailDto);
			}
		} catch (DaoLevelException e) {
			 throw new ServiceException(e.getMessage(), e);
		}
		return 0;
	}
	
	 

	@Override
	public ReturnData<MealOrderDetailDto> findMealOrderDetail(MealOrderDetailDto orderDetailDto, int targetPage, int pageSize)throws ServiceException { 
		try {
			return super.daoFacade.getOrderDao().findMealOrderDetail(orderDetailDto, targetPage, pageSize);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public List<MealOrderDetailDto> findMealOrderDetail(MealOrderDetailDto orderDetailDto) throws ServiceException { 
		try {
			return super.daoFacade.getOrderDao().findMealOrderDetail(orderDetailDto);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	@Override
	public int deteleMealOrderDetailByOrderId(String orderId) throws ServiceException {
		try {
			return super.daoFacade.getOrderDao().deteleMealOrderDetailByOrderId(orderId);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	} 
	
	@Override
	public int deleteMealOrderBy(String orderId) throws ServiceException{
		try {
			return super.daoFacade.getOrderDao().deleteMealOrderBy(orderId);
		} catch (DaoLevelException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
}
